package org.ohos.rajawali3d.examples.examples.ui;

import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.Object3D;
import org.rajawali3d.lights.DirectionalLight;
import org.rajawali3d.loader.LoaderAWD;
import org.rajawali3d.materials.Material;
import org.rajawali3d.materials.methods.DiffuseMethod;
import org.rajawali3d.materials.methods.SpecularMethod;

public class UIElementsFragment extends AExampleFragment {

    @Override
    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        DirectionalLayout ll = new DirectionalLayout(getContext());
        ComponentContainer.LayoutConfig lc = ll.getLayoutConfig();
        lc.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        lc.height = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        ll.setOrientation(DirectionalLayout.VERTICAL);
        ll.setAlignment(LayoutAlignment.CENTER);

        Text label = new Text(getContext());
        label.setText(ResourceTable.String_ui_elements_fragment_text_view_halo_dunia);
        //label.setTextColor(Color.WHITE);
        label.setTextSize(20);
        label.setTextAlignment(TextAlignment.CENTER);
        label.setHeight(100);
        ll.addComponent(label);

        Image image = new Image(getContext());
        ComponentContainer.LayoutConfig lc2 = image.getLayoutConfig();
        lc2.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        lc2.height = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        image.setPixelMap(ResourceTable.Media_rajawali_outline);
        image.setScaleMode(Image.ScaleMode.ZOOM_CENTER);
        ll.addComponent(image);

        mLayout.addComponent(ll);
        mLayout.moveChildToFront(ll);
        mRenderSurface.setTransparent(false);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);

        return mLayout;
    }

    @Override
    public AExampleRenderer createRenderer() {
        return new UIElementsRenderer(getContext(), this);
    }

    private final class UIElementsRenderer extends AExampleRenderer {
        private DirectionalLight mLight;
        private Object3D mMonkey;

        UIElementsRenderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        @Override
        protected void initScene() {
            mLight = new DirectionalLight(0, 0, -1);
            mLight.setPower(.8f);

            getCurrentScene().addLight(mLight);
            getCurrentCamera().setPosition(0, 0, 8);

            try {
                ResourceManager resManager = mContext.getResourceManager();
                RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/awd_suzanne.awd");
                final LoaderAWD parser = new LoaderAWD(mContext.getResourceManager(), mTextureManager, rawFileEntry);
                parser.parse(mContext);

                mMonkey = parser.getParsedObject();

                getCurrentScene().addChild(mMonkey);
            } catch (Exception e) {
                e.printStackTrace();
            }

            Material material = new Material();
            material.enableLighting(true);
            material.setDiffuseMethod(new DiffuseMethod.Lambert());
            material.setSpecularMethod(new SpecularMethod.Phong());
            mMonkey.setMaterial(material);
            mMonkey.setColor(0xff99C224);
        }
    }
}
